﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 22:15:01
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{

    /// <summary>
    ///用户来源 数据访问层
    /// </summary>	
    internal partial class UserSource
    {

        /// <summary>
        /// 根据断言条件获取单个用户来源
        /// </summary>
        public UserSourceInfo QuerySingle(Expression<Func<UserSourceInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取用户来源列表
        /// </summary>
        public IList<UserSourceInfo> QueryList(Expression<Func<UserSourceInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据唯一标识和来源媒体获取信息
        /// </summary>
        /// <param name="sourceToken">唯一标识符</param>
        /// <param name="sourceType">来源媒体</param>
        /// <returns></returns>
        public UserSourceInfo GetByToken(string sourceToken, int sourceType)
        {
            Model.UserSourceInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [UserSource] ");
            strSql.Append("WHERE [SourceToken]=@SourceToken ");
            strSql.Append("AND [SourceType]=@SourceType ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@SourceToken", DbType.String, sourceToken);
            db.AddInParameter(dbc, "@SourceType", DbType.Int32, sourceType);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        public UserSourceInfo GetByUserID(long userID)
        {
            Model.UserSourceInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [UserSource] ");
            strSql.Append("WHERE [UserID]=@UserID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.String, userID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 更新用户头像和昵称
        /// </summary>
        /// <param name="userhead"></param>
        /// <param name="username"></param>
        /// <param name="sourceToken"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public int UpdateUserHead(string userhead, string username, string sourceToken, int sourceType)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            string sql = "UPDATE [UserSource] SET [SourceUserName] = @SourceUserName ,[SourceUserHead] = @SourceUserHead WHERE [SourceToken]=@SourceToken AND [SourceType]=@SourceType";
            DbCommand dbc = db.GetSqlStringCommand(sql);
            db.AddInParameter(dbc, "@SourceUserName", DbType.String, username);
            db.AddInParameter(dbc, "@SourceUserHead", DbType.String, userhead);
            db.AddInParameter(dbc, "@SourceToken", DbType.String, sourceToken);
            db.AddInParameter(dbc, "@SourceType", DbType.Int32, sourceType);
            return db.ExecuteNonQuery(dbc);
        }
    }

}